﻿ 

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Threading;



namespace msBsc.AppLogic.Cfg
{

    /// <summary>

    /// the class for writing something into a file,it can have a buffer to store the data which need to write before write to file really.

    /// </summary>

    class StreamWriteWithBuffer : IDisposable

    {

        string CLimpIDfileName = string.Empty;

        const int CBufferLenght = 10000*38;        

        //static StringBuilder CBuffer = new StringBuilder();



        static Dictionary<int, StringBuilder> CLBufferOfEThread = new Dictionary<int, StringBuilder>();

 

        public StreamWriteWithBuffer(string pfileName)

        {

            CLimpIDfileName = pfileName;

                        

        }

        public StreamWriteWithBuffer()

        {

        }



        ~StreamWriteWithBuffer()

        {

            foreach (StringBuilder valus in CLBufferOfEThread.Values)

            {

                if (valus.Length > 0 && CLimpIDfileName.Length != 0)

                {

                    using (StreamWriter logWrite = new StreamWriter(CLimpIDfileName, true, Encoding.Default))

                    {

                        logWrite.WriteLine(valus.ToString());

                    }

                    valus.Remove(0, valus.Length);

                }

            }

        }




        private static Object lockWrite = new Object();
 
        private static Object lockBuffer = new Object(); 
        public void WriteLine(string pLimpIDfileName, string pstrContent)

        {

            try

            {

                if (CLBufferOfEThread.ContainsKey(Thread.CurrentThread.ManagedThreadId))

                {

                    if (CLBufferOfEThread[Thread.CurrentThread.ManagedThreadId].Length < CBufferLenght)

                    {

                        CLBufferOfEThread[Thread.CurrentThread.ManagedThreadId].AppendLine(pstrContent);

                        CLimpIDfileName = pLimpIDfileName;

                    }

                    else

                    {

                        lock (lockWrite)

                        {

                            using (StreamWriter logWrite = new StreamWriter(pLimpIDfileName, true, Encoding.Default))

                            {

                                logWrite.WriteLine(CLBufferOfEThread[Thread.CurrentThread.ManagedThreadId].ToString());

                            }

                        }



                        CLBufferOfEThread[Thread.CurrentThread.ManagedThreadId].Remove(0, CLBufferOfEThread[Thread.CurrentThread.ManagedThreadId].Length);

                    }

                }

                else

                {

                    lock (lockBuffer)

                    {

                        CLBufferOfEThread[Thread.CurrentThread.ManagedThreadId] = new StringBuilder(pstrContent);

                    }

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine("Exception of limpID log file:" + ex);

            }

        }



        public void Dispose()

        {             

        }

    }

}